﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Globalization;

namespace Pantry.Commons
{
    public sealed class CryptographyHelper
    {
        private CryptographyHelper(){}
        public static string EncryptPassword(string input)
        {
            var mdcsp = new MD5CryptoServiceProvider();
            byte[] b = Encoding.UTF8.GetBytes(input);
            b = mdcsp.ComputeHash(b);
            var sb = new StringBuilder();
            foreach (byte by in b)
            {
                sb.Append(by.ToString("x2", CultureInfo.CurrentCulture).ToLower(CultureInfo.CurrentCulture));
            }
            return sb.ToString();
        }

        public static string Base64Encode(string data)
        {

                byte[] encDataByte = Encoding.UTF8.GetBytes(data);
                string encodedData = Convert.ToBase64String(encDataByte);
                return encodedData;
        }

        public static string Base64Decode(string data)
        {

                var encoder = new UTF8Encoding();
                var utf8Decode = encoder.GetDecoder();

                byte[] todecodeByte = Convert.FromBase64String(data);
                int charCount = utf8Decode.GetCharCount(todecodeByte, 0, todecodeByte.Length);
                var decodedChar = new char[charCount];
                utf8Decode.GetChars(todecodeByte, 0, todecodeByte.Length, decodedChar, 0);
                var result = new String(decodedChar);
                return result;
        }

        public static string EncryptionMD5(string password)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password.Trim(), "SHA1");
        }
    }
}
